package com.ljkj.cordial.chat.dao.notify;


import android.text.TextUtils;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.BusinessLeaveTableDao;
import com.ljkj.cordial.chat.dao.DbController;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.query.QueryBuilder;

import java.io.Serializable;

@Entity(nameInDb = "BusinessLeaveTable")
public class BusinessLeaveTable implements Serializable {

    public final static int AGREE_FRIEND = 17;
    public final static int AGREE_GROUP = 18;
    private static final long serialVersionUID = -1532195397787224303L;
    @Id(autoincrement = true)
    private Long id;
    /**
     * 申请为好友，申请入群，同意为好友，同意入群，拒绝为好友，拒绝入群申请
     */
    @Property(nameInDb = "TARGET_STATUS")
    private int targetStatus;
    /**
     * 多个逗号隔开
     */
    @Property(nameInDb = "AVATAR_URL")
    private String avatarUrl;
    @Property(nameInDb = "AVATAR_ID")
    private String avatarId;
    @Property(nameInDb = "TARGET_USER_ID")
    private String targetUserId;
    @Property(nameInDb = "TARGET_USER_NAME")
    private String targetUserName;
    /**
     * 注册者、游客
     */
    @Property(nameInDb = "TARGET_USER_TYPE")
    private String targetUserType;
    @Property(nameInDb = "TARGET_GROUP_ID")
    private String targetGroupId;
    @Property(nameInDb = "TARGET_GROUP_NAME")
    private String targetGroupName;
    /**
     * (注册者群、游客群)
     */
    @Property(nameInDb = "TARGET_GROUP_TYPE")
    private String targetGroupType;
    @Property(nameInDb = "TARGET_LEAVE")
    private String targetLeave;
    @Index(unique = true)
    @Property(nameInDb = "MSG_ID")
    private String msgId;
    @Property(nameInDb = "IS_READ")
    private int isRead; // 0未读 1已读
    @Property(nameInDb = "LOGIN_USER_ID")
    private String loginUserId;


    @Generated(hash = 1129086413)
    public BusinessLeaveTable(Long id, int targetStatus, String avatarUrl, String avatarId, String targetUserId, String targetUserName, String targetUserType, String targetGroupId, String targetGroupName, String targetGroupType, String targetLeave, String msgId, int isRead,
                              String loginUserId) {
        this.id = id;
        this.targetStatus = targetStatus;
        this.avatarUrl = avatarUrl;
        this.avatarId = avatarId;
        this.targetUserId = targetUserId;
        this.targetUserName = targetUserName;
        this.targetUserType = targetUserType;
        this.targetGroupId = targetGroupId;
        this.targetGroupName = targetGroupName;
        this.targetGroupType = targetGroupType;
        this.targetLeave = targetLeave;
        this.msgId = msgId;
        this.isRead = isRead;
        this.loginUserId = loginUserId;
    }

    @Generated(hash = 787066285)
    public BusinessLeaveTable() {
    }

    public static final int GROUP_LEAVE = 1;
    public static final int FRIEND_LEAVE = 2;

    public static void insert(BusinessLeaveTable table, int leaveType) {
        table.setLoginUserId(ChatApp.getInstance().getUserId());
        try {
            QueryBuilder<BusinessLeaveTable> query = DbController.getInstance().getRead().getBusinessLeaveTableDao().queryBuilder();
            BusinessLeaveTable u;
            if (leaveType == GROUP_LEAVE) {
                u = query.where(BusinessLeaveTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()),
                        BusinessLeaveTableDao.Properties.TargetGroupId.isNotNull(),
                        BusinessLeaveTableDao.Properties.TargetGroupId.eq(table.targetGroupId),
                        BusinessLeaveTableDao.Properties.TargetUserId.isNotNull(),
                        BusinessLeaveTableDao.Properties.TargetUserId.eq(table.targetUserId))
                        .unique();
            } else {
                u = query.where(BusinessLeaveTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()),
                        BusinessLeaveTableDao.Properties.TargetGroupId.isNull(),
                        BusinessLeaveTableDao.Properties.TargetUserId.isNotNull(),
                        BusinessLeaveTableDao.Properties.TargetUserId.eq(table.targetUserId))
                        .unique();
            }

            if (u != null) {
                u.setTargetLeave(table.getTargetLeave());
                u.setTargetGroupId(table.getTargetGroupId());
                u.setTargetGroupName(table.getTargetGroupName());
                u.setTargetGroupType(table.getTargetGroupType());
                u.setTargetUserType(table.getTargetUserType());
                u.setTargetUserId(table.getTargetUserId());
                u.setTargetUserName(table.getTargetUserName());
                u.setAvatarUrl(table.getAvatarUrl());
                u.setTargetStatus(table.getTargetStatus());
                u.setMsgId(table.getMsgId());
                u.setIsRead(0);
                DbController.getInstance().getWrite().getBusinessLeaveTableDao().update(u);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        DbController.getInstance().getWrite().getBusinessLeaveTableDao().insertOrReplace(table);
    }

    public static void delete(String msgId) {
        BusinessLeaveTable u = DbController.getInstance().getRead().getBusinessLeaveTableDao()
                .queryBuilder().where(BusinessLeaveTableDao.Properties.MsgId.eq(msgId),
                        BusinessLeaveTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (u != null) {
            DbController.getInstance().getWrite().delete(u);
        }
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getTargetStatus() {
        return this.targetStatus;
    }

    public void setTargetStatus(int targetStatus) {
        this.targetStatus = targetStatus;
    }

    public String getAvatarUrl() {
        return this.avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getAvatarId() {
        return this.avatarId;
    }

    public void setAvatarId(String avatarId) {
        this.avatarId = avatarId;
    }

    public String getTargetUserId() {
        return this.targetUserId;
    }

    public void setTargetUserId(String targetUserId) {
        this.targetUserId = targetUserId;
    }

    public String getTargetUserName() {
        return this.targetUserName;
    }

    public void setTargetUserName(String targetUserName) {
        this.targetUserName = targetUserName;
    }

    public String getTargetUserType() {
        return this.targetUserType;
    }

    public void setTargetUserType(String targetUserType) {
        this.targetUserType = targetUserType;
    }

    public String getTargetGroupId() {
        return this.targetGroupId;
    }

    public void setTargetGroupId(String targetGroupId) {
        this.targetGroupId = targetGroupId;
    }

    public String getTargetGroupName() {
        return this.targetGroupName;
    }

    public void setTargetGroupName(String targetGroupName) {
        this.targetGroupName = targetGroupName;
    }

    public String getTargetGroupType() {
        return this.targetGroupType;
    }

    public void setTargetGroupType(String targetGroupType) {
        this.targetGroupType = targetGroupType;
    }

    public String getTargetLeave() {
        return this.targetLeave;
    }

    public void setTargetLeave(String targetLeave) {
        this.targetLeave = targetLeave;
    }

    public void setTargetLeave(int state, String memberId, String groupname) {
        switch (state) {
            //消息类型为5，INVITE_TOURISTS,群主邀请游客用户的消息
            case IMSContacts.MsgContentType.INVITE_TOURISTS:
                this.targetLeave = "受邀加入 " + groupname;
                break;
            //消息类型为6，INVITE_REGISTERED，群主邀请注册用户的消息
            case IMSContacts.MsgContentType.INVITE_REGISTERED:
                this.targetLeave = "受邀加入 " + groupname;
                break;
            //消息类型为7，INVITE_REFUSE_TOURISTS,游客用户拒绝群主邀请的消息
            case IMSContacts.MsgContentType.INVITE_REFUSE_TOURISTS:
                this.targetLeave = "拒绝加入 " + groupname;
                break;
            //消息类型为8，INVITE_REFUSE_REGISTERED,注册用户拒绝群主邀请的消息
            case IMSContacts.MsgContentType.INVITE_REFUSE_REGISTERED:
                this.targetLeave = "拒绝加入 " + groupname;
                break;
            //消息类型为9，APPLY_TOURISTS,游客用户申请入群的消息
            case IMSContacts.MsgContentType.APPLY_TOURISTS:
                this.targetLeave = "申请加入 " + groupname;
                break;
            //消息类型为10，APPLY_REGISTERED，注册用户申请入群的消息
            case IMSContacts.MsgContentType.APPLY_REGISTERED:
                this.targetLeave = "申请加入 " + groupname;
                break;
            //消息类型为11，APPLY_REFUSE_TOURISTS,游客群主拒绝申请入群的消息
            case IMSContacts.MsgContentType.APPLY_REFUSE_TOURISTS:
                this.targetLeave = "拒绝你的入群申请";
                break;
            //消息类型为12，APPLY_REFUSE_REGISTERED,注册群主拒绝申请入群的消息
            case IMSContacts.MsgContentType.APPLY_REFUSE_REGISTERED:
                this.targetLeave = "拒绝你的入群申请";
                break;
            //消息类型13，游客用户好友申请
            case IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS:
                this.targetLeave = "申请加为好友";
                break;
            //消息类型14，注册用户好友申请
            case IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED:
                this.targetLeave = "申请加为好友";
                break;
            //消息类型15，游客用户好友申请拒绝
            case IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS:
                this.targetLeave = "申请好友被拒";
                break;
            //消息类型16，注册用户好友申请拒绝
            case IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED:
                this.targetLeave = "申请好友被拒";
                break;
            case IMSContacts.MsgContentType.DELETE_GROUP_MEMBER:
                if (!TextUtils.isEmpty(memberId) && memberId.contains(UserPreferences.getUserId()))
                    this.targetLeave = "你已被移出群" + groupname;
                else
                    this.targetLeave = "被移出群" + groupname;
                break;
            case IMSContacts.MsgContentType.EXIT_GROUP:
                this.targetLeave = "退出了群" + groupname;
                break;
            case IMSContacts.MsgContentType.DISSOLVE_GROUP:
                this.targetLeave = "解散了群" + groupname;
                break;
        }
    }

    public String getMsgId() {
        return this.msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String getLoginUserId() {
        return this.loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    public int getIsRead() {
        return this.isRead;
    }

    public void setIsRead(int isRead) {
        this.isRead = isRead;
    }


}
